package com.wei.duowan.bbs.widget;

import android.content.Intent;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.model.WebViewBean;
import com.wei.duowan.bbs.ui.activity.BaseActivity;
import com.wei.duowan.bbs.ui.activity.WebViewActivity;

/**
 * 作者：赵若位
 * 时间：2019/9/6 14:52
 * 联系方式：1070138445
 * 功能：TextView点击跳转WebView
 */
public class TextClickableSpan extends ClickableSpan
{
    private WebViewBean mWebViewBean;
    private int mColor;


    public TextClickableSpan(@Nullable WebViewBean data)
    {
        if (data == null)
        {
            throw new NullPointerException("The WebViewBean is Empty!");
        }
        this.mWebViewBean = data;
    }

    public void setColor(@ColorInt int color)
    {
        this.mColor = color;
    }


    @Override
    public void onClick(View view)
    {
        if (view.getContext() instanceof BaseActivity)
        {
            ((BaseActivity) view.getContext()).openActivity(WebViewActivity.class, mWebViewBean);
            return;
        }
        Intent it = new Intent(view.getContext(), WebViewActivity.class);
        it.putExtra(WebViewActivity.TAG, mWebViewBean);
        view.getContext().startActivity(it);
    }

    @Override
    public void updateDrawState(@NonNull TextPaint ds)
    {
        super.updateDrawState(ds);
        ds.setColor(mColor <= 0 ? App.getResource().getColor(R.color.colorClick) : mColor);
        ds.setUnderlineText(false);
        ds.clearShadowLayer();
    }


}
